/**
 * @author 徐楠
 * @date 2022/5/11 9:00
 * @version 1.0
 */

package com.xunan.likou;

public class IntegerConversion {
    public static void main(String[] args) {
        //Twelve Million Three Hundred Forty Five Thousand Six Hundred Seventy Eight
        //int num = 12345678;
        int num = 123456789;
        String s = numberToWords(num);
        System.out.println(s);
    }


    final static String[] singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    final static String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    final static String[] tennsty = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    final static String[] thousands = {"", "Thousand", "Million", "Billion"};

    public static String numberToWords(int num) {
        if (num == 0) {
            return "Zero";
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 3, unit = 1000000000; i >= 0; i--, unit /= 1000) {
            int curNum = num / unit;
            System.out.println("num  " + num);
            System.out.println("unit " + unit);
            System.out.println("curNum " + curNum);
            if (curNum != 0) {
                num -= curNum * unit;
                System.out.println("传进去的curNum " + curNum);
                sb.append(toEnglish(curNum)).append(thousands[i]).append(" ");
            }
            System.out.println("-----------");
        }
        //舍去头之前 尾之后的空格
        return sb.toString().trim();
    }

    public static String toEnglish(int num) {
        StringBuffer curr = new StringBuffer();
        //取百位
        int hundred = num / 100;
        if (hundred != 0) {
            curr.append(singles[hundred]).append(" Hundred ");
        }
        //取十位
        num %= 100;//num已经变成了两位数
        int ten = num / 10;
        System.out.println("ten " + ten);
        System.out.println("num 一： " + num);

        if (ten >= 2) {
            curr.append(tennsty[ten]).append(" ");
            num %= 10;
        }

        System.out.println("num 二： " + num);

        if (num > 0 && num < 10) {
            curr.append(singles[num]).append(" ");
        } else if (num >= 10) {
            curr.append(teens[num - 10]).append(" ");
        }
        return curr.toString();
    }

}
